import React from "react";
import { inject, observer } from "mobx-react";
import { message } from "antd";

const HOCStoreBasicSet = (WrapComponent) => {
  @inject("setStoreInfo")
  @observer
  class ElementComponent extends React.Component {
    state = {
      bsDgpd: [], //商品默认图
      bsDpd: [], //团购默认图
    };
    componentDidMount() {
      let { getStoreBasicSet } = this.props.setStoreInfo;
      //查询基本设置
      getStoreBasicSet().then((data) => {
        if (data.data.code === 200) {
          let allData = data.data.data[0];
          let obj = this.state;
          obj.bsDgpd = [
            {
              thumbUrl: allData.bsDgpd,
            },
          ];
          obj.bsDpd = [
            {
              thumbUrl: allData.bsDpd,
            },
          ];
          this.setState(obj);
          this.setValues(allData);
        } else {
          message.error("请求数据失败");
        }
      });
    }
    setValues = (data) => {
      let { formOne, formTwo } = this.refs.form.refs;
      formOne.setFieldsValue(data);
      formTwo.setFieldsValue(data);
    };
    beforeUploadImg = (file, attr) => {
      const isJPG = file.type === "image/jpeg";
      const isJPEG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const size = file.size / 1024 / 1024;
      if (!(isJPG || isJPEG || isPNG)) {
        message.error("错误，图片格式应为jpg/jpge/png");
        return false;
      } else if (size >= 2) {
        message.warning("图片应<=2M");
        return false;
      } else {
        //验证成功后
        //制作图片缩略图
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = (e) => {
          file.thumbUrl = e.target.result;
          let obj = this.state;
          obj[attr] = [];
          obj[attr].push(file);
          this.setState(obj);
        };
      }
      return false;
    };
    handleRemove = (file, attr) => {
      let obj = this.state;
      obj[attr] = [];
      this.setState(obj);
    };
    submit = async () => {
      let { formOne, formTwo } = this.refs.form.refs;
      let { updateStoreBasicSet } = this.props.setStoreInfo;
      let bsDgpd = this.state.bsDgpd[0]
      let bsDpd = this.state.bsDpd[0]
      if (bsDgpd.name) {
        bsDgpd = await this.upLoadImg(bsDgpd)
      }else{
        bsDgpd = bsDgpd.thumbUrl
      }
      if (bsDpd.name) {
          bsDpd = await this.upLoadImg(bsDpd)
      }else{
        bsDpd = bsDpd.thumbUrl
      }
      let bsWebname = formOne.getFieldValue("bsWebname"); //网站域名
      let bsInstall = formOne.getFieldValue("bsInstall"); //安装目录
      let bsLain = formOne.getFieldValue("bsLain"); //引导关注链接
      let bsEi = formOne.getFieldValue("bsEi"); //启用库存
      let bsCpname = formOne.getFieldValue("bsCpname"); //消费积分名称
      let bsInp = formOne.getFieldValue("bsInp"); //商品货号前缀
      let bsReviewStatus = formOne.getFieldValue("bsReviewStatus"); //评论审核

      let bsCtime = formTwo.getFieldValue("bsCtime"); //缓存时间
      let bsSc = formTwo.getFieldValue("bsSc"); //统计代码
      let bsGzipmode = formTwo.getFieldValue("bsGzipmode"); //是否启用Gzip模式
      let bsRs = formTwo.getFieldValue("bsRs"); //排行统计
      let bsDi = formTwo.getFieldValue("bsDi"); //默认库存
      let bsLiStatus = formTwo.getFieldValue("bsLiStatus"); //物流跟踪

      let obj = {
        bsId: 1,
        bsDgpd,
        bsDpd,
        bsWebname,
        bsInstall,
        bsLain,
        bsEi,
        bsCpname,
        bsInp,
        bsReviewStatus,
        bsCtime,
        bsSc,
        bsGzipmode,
        bsRs,
        bsDi,
        bsLiStatus,
      };
      updateStoreBasicSet(obj).then((data) => {
        if (data.data.code === 200) {
          message.success("更新数据成功");
        } else {
          message.error("更新数据失败");
        }
      });
    };

    upLoadImg = async (data) => {
      let { getPhotos } = this.props.setStoreInfo;
      let formData = new FormData();
      formData.append("file", data);
      let url = await getPhotos(formData).then((data) => {
        if (data.data.code === 200) {
          return data.data.data[0];
        } else {
          message.error("上传图片错误");
        }
      });
      return url;
    };
    render() {
      let method = {
        beforeUploadImg: this.beforeUploadImg,
        handleRemove: this.handleRemove,
        submit: this.submit,
      };
      return (
        <div>
          <WrapComponent ref="form" {...this.state} {...method}></WrapComponent>
        </div>
      );
    }
  }
  return ElementComponent;
};

export default HOCStoreBasicSet;
